var vm = new Vue({
    el:"#app",
    data () {
        return {
            //初始化
            UserNo:'',
            UserName:'',
            Store:'',
            Bm:'',
            NowDate:'',
            //搜索条件
            QStore:'',
            DocType:'',
            ProductNameOrNo:'',
            //新增弹框参数
            formItem: {
                OutNo: '',
                InNo:'',
                ProductNo:'',
                ProductName:'',
                WarehouseNo:'',
                WarehouseName:'',
                InputMan:'',
                OutNumber:1,
                OutPrice:'',
                Store:'',
                DocType:'',
                ZPUserDocNo:'',
                ZPOutUser: '',
                ZPZwNo:'',
                ZPTHUserDocNo:'',
                ZPTHOutUser: '',
                ZPTHZwNo:'',
                DBOutUser:'',
                CGOutUserNo:'',
                CGOutUserName:''
            },
            modal1: false, //出库库弹框
            level1: true, //调拨店面
            rkmodel:false,//入库单据弹框
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '出库编号',
                    key: 'OutNo',
                    align: 'center',
                },
                {
                    title: '店面',
                    key: 'Store',
                    align: 'center',
                },
                {
                    title: '出库类型',
                    key: 'DocType',
                    align: 'center',
                    render:(h,params) => {
                        if(params.row.DocType=='1'){
                            return h("span","赠品出库");
                        }else if(params.row.DocType=='2'){
                            return h("span","赠品退货");
                        }else if(params.row.DocType=='3'){
                            return h("span","调拨出库");
                        }else if(params.row.DocType=='4'){
                            return h("span","采购退货");
                        }
                    }
                },
                {
                    title: '产品编号',
                    key: 'ProductNo',
                    align: 'center',
                },
                {
                    title: '产品名称',
                    key: 'ProductName',
                    align: 'center',
                },
                {
                    title: '仓库编号',
                    key: 'WarehouseNo',
                    align: 'center',
                },
                {
                    title: '仓库名称',
                    key: 'WarehouseName',
                    align: 'center',
                },
                {
                    title: '出库数量',
                    key: 'OutNumber',
                    align: 'center',
                    width:95
                },
                {
                    title: '入库单据编号',
                    key: 'InNo',
                    align: 'center',
                },
                {
                    title: '出库时间',
                    key: 'CreateTime',
                    align: 'center',
                    width:95
                },
                {
                    title: '出库对象',
                    key: 'OutUser',
                    align: 'center',
                },
                {
                    title: '销售票号',
                    key: 'UserDocNo',
                    align: 'center',
                    render:(h,params) => {
                        if(params.row.UserDocNo==''||params.row.UserDocNo==null){
                            return h("span","-");
                        }else{
                            return h("span",params.row.UserDocNo);
                        }
                    }
                },
                {
                    title: '展位编号',
                    key: 'ZwNo',
                    align: 'center',
                    render:(h,params) => {
                        if(params.row.ZwNo==''||params.row.ZwNo==null){
                            return h("span","-");
                        }else{
                            return h("span",params.row.ZwNo);
                        }
                    }
                },
                {
                    title: '经手人',
                    key: 'UserName',
                    align: 'center',
                },

                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            /*h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        this.edit();
                                    }
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),*/
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        var OutNo = params.row.OutNo;
                                        var InNo = params.row.InNo;
                                        var OutNumber = params.row.OutNumber;
                                        var CreateTime = params.row.CreateTime;
                                        var InputMan = params.row.InputMan;
                                        var CreateTimeArr = CreateTime.split(" ");
                                        CreateTime = CreateTimeArr[0];
                                        if (CreateTime!=self.NowDate){
                                            toastr.warning("只允许操作当日单据");
                                            return false;
                                        }
                                        if (InputMan!=self.UserNo){
                                            toastr.warning("只允许操作自己添加的单据");
                                            return false;
                                        }
                                        this.confirm(OutNo,InNo,OutNumber,CreateTime);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            ckTotal:0,
            ckCurrent:1,
            ckPageSize:15,
            data1: [],
            //选择产品弹框
            queryForm:{
                DealerNo:'',
                ProductName:'',
                WarehouseNo:'',
                Type:'',
                InNo:''
            },
            //经销商
            Dealers:[],
            //仓库
            wareHouses:[],
            rkcolumns: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '入库单号',
                    key: 'InNo',
                    align: 'center',
                },
                {
                    title: '入库类型',
                    key: 'Type',
                    align: 'center',
                    render:(h,params) => {
                        if(params.row.Type=='0'){
                            return h("span","采购入库");
                        }else if(params.row.Type=='1'){
                            return h("span","调拨入库");
                        }
                    }
                },
                {
                    title: '经销商编号',
                    key: 'DealerNo',
                    align: 'center',
                },
                {
                    title: '经销商名称',
                    key: 'dalerName',
                    align: 'center',
                },
                {
                    title: '产品编号',
                    key: 'ProductNo',
                    align: 'center',
                },
                {
                    title: '产品名称',
                    key: 'ProductName',
                    align: 'center',
                },
                /*{
                    title: '产品规格',
                    key: 'Specification',
                    align: 'center',
                },
                {
                    title: '包装规格',
                    key: 'Package',
                    align: 'center',
                },*/
                {
                    title: '产品型号',
                    key: 'Type',
                    align: 'center',
                },
                {
                    title: '产品单价',
                    key: 'ProductPrice',
                    align: 'center',
                },
                {
                    title: '产品数量',
                    key: 'ProductNumber',
                    align: 'center',
                },
                {
                    title: '剩余数量',
                    key: 'RestNumber',
                    align: 'center',
                },
                /*{
                    title: '仓库编号',
                    key: 'WarehouseNo',
                    align: 'center',
                },*/
                {
                    title: '仓库名称',
                    key: 'warehouseName',
                    align: 'center',
                },
            ],
            rkdata: [],
            rkTotal:0,
            rkCurrent:1,
            rkPageSize:15,

        }
    },
    created(){
        this.initThisPage();
        this.getDealer();
        this.getWh();
    },
    methods: {
        ok () {
        },
        cancel () {
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }else{
                        self.UserNo = data.UserNo;
                        self.UserName = data.UserName;
                        self.Store = data.Store;
                        self.Bm = data.Bm;
                        self.NowDate=data.nowdate;
                        if(data.Store == '运营管理中心'){
                            $("#StoreIsOrNot").attr("style","");
                        }else{
                            self.QStore = data.Store;
                            $("#StoreIsOrNot").attr("style","display:none");
                        }
                        if(data.Bm!="办公室"){
                            $("#CKIsOrNot").attr("style","display:none");
                        }
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        //打开出库弹框
        openModel1(){
            this.formItem.OutNo = "CK-"+this.uuid(8, 16);
            this.formItem.InputMan = this.UserName;
            this.formItem.Store = this.Store;
            this.modal1 = true;
        },
        //打开入库弹框
        openrkmodel(){
            var self = this;
            self.rkmodel = true;
            /*var info ;
            var Store = self.Store;
            var jsondata={"Store":Store};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goOutBound/queryAllInBound", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                    self.rkmodel = true;
                },
                error: function() {},
            });
            if(info){
                self.rkdata = info;
            }*/
        },
        rkbeforequerytable(current){
            var self = this;
            self.rkCurrent = current;
            self.queryIn();
        },
        //查询入库信息
        queryIn(){
            var self = this;
            this.queryForm.Store=this.Store;
            var rkTotal = 0;
            var rkCurrent = self.rkCurrent;
            var rkPageSize = self.rkPageSize;
            var jsondata = this.queryForm;
            jsondata.rkCurrent = rkCurrent;
            jsondata.rkPageSize = rkPageSize;
            var data_json = JSON.stringify(this.queryForm);
            $.ajax({
                url: "./goInBound/queryAllInBound", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    self.rkdata = data.ResultList;
                    rkTotal = data.rkTotal;
                    self.rkTotal = rkTotal;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //获取经销商
        getDealer(){
            var jsondata = {}
            var data_json = JSON.stringify(jsondata);
            var self = this;
            $.ajax({
                url: "./goJxs/queryAllJxs", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    self.Dealers=data.ResultList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //获取仓库
        getWh(){
            var jsondata = {}
            var data_json = JSON.stringify(jsondata);
            var self = this;
            $.ajax({
                url: "./goWh/queryWhByParm", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    self.wareHouses=data.ResultList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        RowClic(CurrentRow){
            this.formItem.WarehouseName = CurrentRow.warehouseName;
            this.formItem.WarehouseNo = CurrentRow.WarehouseNo;
            this.formItem.ProductName = CurrentRow.ProductName;
            this.formItem.ProductNo = CurrentRow.ProductNo;
            this.formItem.InNo = CurrentRow.InNo;
            this.formItem.CGOutUserNo = CurrentRow.DealerNo;
            this.formItem.CGOutUserName = CurrentRow.dalerName;
            this.rkmodel = false;
        },
        //通过出库类型改变存入参数div
        changeSaveCkDiv(){
            var self = this;
            var DocType = self.formItem.DocType;
            if(DocType=="1"){
                $("#TypeOne").attr("style","");
                $("#TypeTwo").attr("style","display:none");
                $("#TypeThree").attr("style","display:none");
                $("#TypeFour").attr("style","display:none");
            }else if(DocType=="2"){
                $("#TypeTwo").attr("style","");
                $("#TypeOne").attr("style","display:none");
                $("#TypeThree").attr("style","display:none");
                $("#TypeFour").attr("style","display:none");
            }else if(DocType=="3"){
                $("#TypeThree").attr("style","");
                $("#TypeOne").attr("style","display:none");
                $("#TypeTwo").attr("style","display:none");
                $("#TypeFour").attr("style","display:none");
            }else if(DocType=="4"){
                $("#TypeFour").attr("style","");
                $("#TypeOne").attr("style","display:none");
                $("#TypeThree").attr("style","display:none");
                $("#TypeTwo").attr("style","display:none");
            }
        },
        //添加出库信息
        saveOutInfo(){
            var self = this;
            var OutNo = self.formItem.OutNo;
            var InputMan = self.UserNo;
            var DocType = self.formItem.DocType;
            var ProductNo = self.formItem.ProductNo;
            var ProductName = self.formItem.ProductName;
            var OutNumber = self.formItem.OutNumber;
            var WarehouseName = self.formItem.WarehouseName;
            var WarehouseNo = self.formItem.WarehouseNo;
            var InNo = self.formItem.InNo;
            var Store = self.Store;
            if(Store=="运营管理中心"){
                toastr.warning("总部无出库权限");
                return false;
            }
            if(DocType==null||DocType==""){
                toastr.warning("请选择单据类型");
                return false;
            }
            if(ProductNo==null||ProductNo==""){
                toastr.warning("请选择产品");
                return false;
            }
            if(OutNumber==null||OutNumber==""){
                toastr.warning("请录入出库数量");
                return false;
            }
            if(WarehouseNo==null||WarehouseNo==""){
                toastr.warning("出库仓库不存在");
                return false;
            }
            if(InNo==null||InNo==""){
                toastr.warning("入库单据编号不存在");
                return false;
            }
            var jsondata = {
                "OutNo":OutNo,
                "InputMan":InputMan,
                "DocType":DocType,
                "ProductNo":ProductNo,
                "ProductName":ProductName,
                "OutNumber":OutNumber,
                "WarehouseNo":WarehouseNo,
                "WarehouseName":WarehouseName,
                "InNo":InNo,
                "Store":Store
            }
            var ZPOutUser = "";
            var ZPUserDocNo = "";
            var ZPZwNo = "";
            var ZPTHOutUser = "";
            var ZPTHUserDocNo = "";
            var ZPTHZwNo = "";
            var DBOutUser = "";
            var CGOutUserNo = "";
            var CGOutUserName = "";
            if(DocType=="1"){
                ZPOutUser = self.formItem.ZPOutUser;
                ZPOutUser = ZPOutUser.trim();
                ZPUserDocNo = self.formItem.ZPUserDocNo;
                ZPUserDocNo = ZPUserDocNo.trim();
                ZPZwNo = self.formItem.ZPZwNo;
                ZPZwNo = ZPZwNo.trim();
                if(ZPUserDocNo==null||ZPUserDocNo==""){
                    toastr.warning("请录入销售票号");
                    return false;
                }
                if(ZPOutUser==null||ZPOutUser==""){
                    toastr.warning("请录入顾客");
                    return false;
                }
                if(ZPZwNo==null||ZPZwNo==""){
                    toastr.warning("请录入展位编号");
                    return false;
                }
                jsondata.ZPOutUser = ZPOutUser;
                jsondata.ZPUserDocNo = ZPUserDocNo;
                jsondata.ZPZwNo = ZPZwNo;
            }else if(DocType=="2"){
                ZPTHOutUser = self.formItem.ZPTHOutUser;
                ZPTHOutUser = ZPTHOutUser.trim();
                ZPTHUserDocNo = self.formItem.ZPTHUserDocNo;
                ZPTHUserDocNo = ZPTHUserDocNo.trim();
                ZPTHZwNo = self.formItem.ZPTHZwNo;
                ZPTHZwNo = ZPTHZwNo.trim();
                if(ZPTHUserDocNo==null||ZPTHUserDocNo==""){
                    toastr.warning("请录入销售票号");
                    return false;
                }
                if(ZPTHOutUser==null||ZPTHOutUser==""){
                    toastr.warning("请录入顾客");
                    return false;
                }
                if(ZPTHZwNo==null||ZPTHZwNo==""){
                    toastr.warning("请录入展位编号");
                    return false;
                }
                jsondata.ZPTHOutUser = ZPTHOutUser;
                jsondata.ZPTHUserDocNo = ZPTHUserDocNo;
                jsondata.ZPTHZwNo = ZPTHZwNo;
            }else if(DocType =="3"){
                DBOutUser = self.formItem.DBOutUser;
                if(DBOutUser==null||DBOutUser==""){
                    toastr.warning("请录入调拨店面");
                    return false;
                }
                jsondata.DBOutUser = DBOutUser;
            }else if(DocType =="4"){
                CGOutUserNo = self.formItem.CGOutUserNo;
                if(CGOutUserNo==null||CGOutUserNo==""){
                    toastr.warning("请录入经销商");
                    return false;
                }
                CGOutUserName = self.formItem.CGOutUserName;
                jsondata.CGOutUserNo = CGOutUserNo;
                jsondata.CGOutUserName = CGOutUserName;
            }



            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goOutBound/saveOutBound", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.formItem.OutNo = "";
                        self.formItem.InNo = "";
                        self.formItem.ProductNo = "";
                        self.formItem.ProductName = "";
                        self.formItem.WarehouseNo = "";
                        self.formItem.WarehouseName = "";
                        self.formItem.InputMan = "";
                        self.formItem.OutNumber = 1;
                        self.formItem.OutPrice = "";
                        self.formItem.Store = "";
                        self.formItem.DocType = "";
                        self.formItem.ZPUserDocNo = "";
                        self.formItem.ZPOutUser = "";
                        self.formItem.ZPZwNo = "";
                        self.formItem.ZPTHUserDocNo = "";
                        self.formItem.ZPTHOutUser = "";
                        self.formItem.ZPTHZwNo = "";
                        self.formItem.DBOutUser = "";
                        self.formItem.CGOutUserNo = "";
                        self.formItem.CGOutUserName = "";
                        self.querytable();
                        self.queryIn();
                    }else if(data.Status=='-1'){
                        toastr.warning('保存失败，经销商已存在');
                    }else if(data.Status=='-2'){
                        toastr.warning('保存失败，库存不足');
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        ckbeforequerytable(current){
            var self = this;
            self.ckCurrent = current;
            self.querytable();
        },
        querytable(){
            var self = this;
            var info ;
            var Store = self.QStore;
            var DocType = self.DocType;
            var ProductNameOrNo = self.ProductNameOrNo;
            var ckTotal = 0;
            var ckCurrent = self.ckCurrent;
            var ckPageSize = self.ckPageSize;
            var jsondata={"DocType":DocType,"ProductNameOrNo":ProductNameOrNo,"Store":Store,"ckCurrent":ckCurrent,"ckPageSize":ckPageSize};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goOutBound/queryAllOutBound", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                    ckTotal = data.ckTotal;
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
                self.ckTotal = ckTotal;
            }
        },
        //确定是否删除出库信息
        confirm (OutNo,InNo,OutNumber) {
            var self = this;
            this.$Modal.confirm({
                title: '提示',
                content: '<p>是否删除</p>',
                onOk: () => {
                    self.delOutBound(OutNo,InNo,OutNumber);
                },
                onCancel: () => {
                    toastr.info("取消删除");
                }
            });
        },
        //删除出库信息
        delOutBound(OutNo,InNo,OutNumber){
            var self = this;
            var jsondata = {
                "OutNo":OutNo,
                "InNo":InNo,
                "OutNumber":OutNumber
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goOutBound/delOutBound", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success("删除成功");
                        self.querytable();
                        self.queryIn();
                    }else{
                        toastr.error('删除失败，请刷新页面后重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        // 编辑弹框
        edit(){
            this.modal1=true;
        },

        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //uuid
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        }
    }
})